// pages/myorders/myorders.js
var ShopID = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    wulimodel: true,
    active1: true,
    active2: false,
    active3: false,
    active4: false,
    active5: false,
    active6: false,
    PageIndex: 1,
    PageSize: 3,
    OrderStatus: 'All',
    OrderList: {},
    IsLastPage: false,
    nodata: true,
    ShopID: 0
  },
  wulimodel: function() {
    this.setData({
      wulimodel: false,
    })
  },
  wuliclose: function() {
    this.setData({
      wulimodel: true,
    })
  },
  tabchang1: function() {
    this.setData({
      active1: true,
      active2: false,
      active3: false,
      active4: false,
      active5: false,
      active6: false,
      PageIndex: 1,
      OrderStatus: 'All',
    })
    LoadOrderList(this, this.data.OrderStatus, 1);
  },
  tabchang2: function() {
    this.setData({
      active1: false,
      active2: true,
      active3: false,
      active4: false,
      active5: false,
      active6: false,
      PageIndex: 1,
      OrderStatus: 'WaitPay',
    })
    LoadOrderList(this, this.data.OrderStatus, 1);
  },
  tabchang3: function() {
    this.setData({
      active1: false,
      active2: false,
      active3: true,
      active4: false,
      active5: false,
      active6: false,
      PageIndex: 1,
      OrderStatus: 'WaitDeliver',
    })
    LoadOrderList(this, this.data.OrderStatus, 1);
  },
  tabchang4: function() {
    this.setData({
      active1: false,
      active2: false,
      active3: false,
      active4: true,
      active5: false,
      active6: false,
      PageIndex: 1,
      OrderStatus: 'WaitReceipt',
    })
    LoadOrderList(this, this.data.OrderStatus, 1);
  },
  tabchang5: function() {
    this.setData({
      active1: false,
      active2: false,
      active3: false,
      active4: false,
      active5: true,
      active6: false,
      PageIndex: 1,
      OrderStatus: 'WaiteValuate',
    })
    LoadOrderList(this, this.data.OrderStatus, 1);
  },
  tabchang6: function() {
    this.setData({
      active1: false,
      active2: false,
      active3: false,
      active4: false,
      active5: false,
      active6: true,
      PageIndex: 1,
      OrderStatus: 'Refund',
    })
    LoadOrderList(this, this.data.OrderStatus, 1);
  },
  linkTo: function() {

    wx.switchTab({
      url: '/pages/customIndex/index'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options.shopID > 0) {
      this.data.ShopID = options.ShopID;
      ShopID = options.ShopID;
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

    var res = wx.getStorageSync('UserInfo');
    if (res == "") {
      wx.redirectTo({
        url: '/pages/login/login',
      })
      return false;
    }
    var that = this;
    var Status = that.data.OrderStatus;
    var orderState = wx.getStorageSync('OrderStatusUserInfo');

    if (orderState == '0') {
      that.setData({
        active1: true,
        active2: false,
        active3: false,
        active4: false,
        active5: false,
        active6: false,
      })
      Status = 'All';
    } else if (orderState == '1') {
      that.setData({
        active1: false,
        active2: true,
        active3: false,
        active4: false,
        active5: false,
        active6: false,
      })
      Status = 'WaitPay';
    } else if (orderState == '2') {
      that.setData({
        active1: false,
        active2: false,
        active3: true,
        active4: false,
        active5: false,
        active6: false,
      })
      Status = 'WaitDeliver';
    } else if (orderState == '3') {
      that.setData({
        active1: false,
        active2: false,
        active3: false,
        active4: true,
        active5: false,
        active6: false,
      })
      Status = 'WaitReceipt';
    } else if (orderState == '4') {
      that.setData({
        active1: false,
        active2: false,
        active3: false,
        active4: false,
        active5: true,
        active6: false,
      })
      Status = 'WaiteValuate';
    } else if (orderState == '5') {
      that.setData({
        active1: false,
        active2: false,
        active3: false,
        active4: false,
        active5: false,
        active6: true,
      })
      Status = 'Refund';
    }

    LoadOrderList(this, Status, 1);


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    //wx.removeStorageSync('OrderStatusUserInfo');
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.stopPullDownRefresh();

    if (this.data.PageIndex == 1) {
      LoadOrderList(this, this.data.OrderStatus, 1);

      return false;
    }

    this.data.IsLastPage = false;
    LoadOrderList(this, this.data.OrderStatus, --this.data.PageIndex);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.IsLastPage && this.data.PageIndex != 1) {
      wx.showToast({
        title: '已到底',
        duration: 2000
      })
      return false;
    }
    LoadOrderList(this, this.data.OrderStatus, ++this.data.PageIndex);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },



  /**
   * 跳转订单详情
   */
  onRedirectToOrderInfo: function(event) {

    wx.navigateTo({
      url: '/pages/orderDetails/orderDetails?OrderID=' + event.currentTarget.id + "&shopID=" + event.currentTarget.dataset.shopid
    })
  }

})

//获取订单列表
function LoadOrderList(obj, OrderStatus, PageIndex) {
  if (ShopID <= 0) {
    ShopID = -1;
  }
  wx.showLoading({
    title: '加载中...',
    mask: true
  });

  var globalData = getApp().globalData;
  var custId = globalData.custID;
  var res = wx.getStorageSync('UserInfo');
  var userId = res.UserID;
  var pageSize = obj.data.PageSize;
  var url = globalData.apiurl + '/order/api/OrderInfo/GetMyOrderList';

  wx.request({
    url: url,
    data: {
      "custId": custId,
      "userId": userId,
      "orderStatus": OrderStatus,
      "pageIndex": PageIndex,
      "pageSize": pageSize,
      "UnionStoreID": ShopID,
      "OrderType": -1
    },
    method: 'GET',
    header: {
      'content-type': 'application/json'
    },
    success: function(res) {
      if (obj.data.PageIndex = 1 && res.data.content.length == 0) {
        obj.setData({
          nodata: false
        });
      } else {
        obj.setData({
          nodata: true
        });
      }
      var list = res.data.content;
      if (list != null) {
        for (var i = 0, len = list.length; i < len; i++) {
          if (list[i].OrderType == 1) {
            if (list[i].TeamStatus < 2) {
              list[i].TeamDescribe = list[i].StatusStr + "，" + list[i].TeamNum + "人拼团，差" + list[i].LackNum + "人";
            } else
            if (list[i].TeamStatus == 2) {
              list[i].TeamDescribe = list[i].StatusStr + "，" + list[i].TeamNum + "人拼团，拼团成功";
            } else
            if (list[i].TeamStatus == 3) {
              list[i].TeamDescribe = list[i].StatusStr + "，" + list[i].TeamNum + "人拼团，拼团失效";
            }
          } else {
            list[i].TeamDescribe = list[i].StatusStr;
          }
        }
      }
      if (res.data.content.length < pageSize) {
        obj.data.IsLastPage = true;
        obj.setData({
          OrderList: list
        })
        wx.hideLoading();

        return false;
      }
      obj.setData({
        OrderList: list
      })
      wx.hideLoading();
    },
    fail: function() {
      wx.hideLoading();
      wx.showToast({
        title: '获取列表失败',
        duration: 2000
      })
    },
    complete: function() {
      //complete
    }
  })
}